package com.lenovo.smarttraffic.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.DatePickerBean;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Caleb.F
 * on 2020-03-08
 * at 23:49
 */
public class RangeDateDialog extends DialogFragment {
    private static final String TAG = "RangeDateDialog";
    private ViewHolder myViewHolder;
    private long endTimeMillis = 0;
    private long startTimeMillis = 0;
    private String startString;
    private String endString;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_range_data, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myViewHolder = new ViewHolder(view);
        initCalendar();
        initView();
    }

    private void initView() {
        myViewHolder.mTvCancel.setOnClickListener(v -> {
            dismiss();
        });
        myViewHolder.mTvConfirm.setOnClickListener(v -> {
            if (startTimeMillis == 0 || endTimeMillis == 0) {
                Toast.makeText(getActivity(), "请选择日期", Toast.LENGTH_SHORT).show();
            } else {
                if (startTimeMillis > endTimeMillis) {
                    EventBus.getDefault().post(new DatePickerBean(endTimeMillis, startTimeMillis, endString, startString));
                } else {
                    EventBus.getDefault().post(new DatePickerBean(startTimeMillis, endTimeMillis, startString, endString));
                }
                dismiss();
            }
        });
        myViewHolder.mTvYear.setOnClickListener(v -> {
            DateYearDialogFragment dataPickerDialog = DateYearDialogFragment.newInstance(
                    new StringBuilder().append(myViewHolder.mTvYear.getText()).append(",")
                            .append(myViewHolder.mTvDay.getText()).append(",")
                            .append(myViewHolder.mTvWeek.getText()).toString());
            dataPickerDialog.setonItemSelectListener(new DateYearDialogFragment.onItemSelectListener() {
                @Override
                public void getYear(String year) {
                    myViewHolder.mTvYear.setText(year);
                    myViewHolder.mCalendarView.scrollToCalendar(Integer.parseInt(year),
                            myViewHolder.mCalendarView.getSelectedCalendar().getMonth(),
                           myViewHolder.mCalendarView.getSelectedCalendar().getDay());
                    String s = myViewHolder.mTvCorrentDate.getText().toString();
                    myViewHolder.mTvCorrentDate.setText(s.replace(s.substring(0,4), String.valueOf(year)));

                }
            });
            getChildFragmentManager().beginTransaction().add(dataPickerDialog,"").commit();
        });
    }


    private void initCalendar() {
        myViewHolder.mCalendarView.scrollToCalendar(2016, 5, 1);

        myViewHolder.mImPre.setOnClickListener(v -> {
            myViewHolder.mCalendarView.scrollToPre();
            Calendar selectedCalendar = myViewHolder.mCalendarView.getSelectedCalendar();
            myViewHolder.mTvYear.setText(String.valueOf(selectedCalendar.getYear()));
            myViewHolder.mTvDay.setText(new StringBuilder().append(selectedCalendar.getMonth())
                    .append("月").append(1).append("日").toString());
        });
        myViewHolder.mImNex.setOnClickListener(v ->
        {
            myViewHolder.mCalendarView.scrollToNext();
            Calendar selectedCalendar = myViewHolder.mCalendarView.getSelectedCalendar();
            myViewHolder.mTvDay.setText(new StringBuilder().append(selectedCalendar.getMonth())
                    .append("月").append(1).append("日").toString());
        });

        myViewHolder.mCalendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                myViewHolder.mTvCorrentDate.setText(new StringBuilder().append(year).append("年")
                        .append(month).append("月").append(1).append("日").toString());
            }
        });
        myViewHolder.mCalendarView.setOnCalendarRangeSelectListener(new CalendarView.OnCalendarRangeSelectListener() {
            @Override
            public void onCalendarSelectOutOfRange(Calendar calendar) {
                Toast.makeText(getActivity(), "当前日期超出范围，请选择2015-1-1至2019-1-1之间的日期", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSelectOutOfRange(Calendar calendar, boolean isOutOfMinRange) {
                //设置的无限制，不需要操作
            }

            @Override
            public void onCalendarRangeSelect(Calendar calendar, boolean isEnd) {
                if (isEnd) {
                    endTimeMillis = calendar.getTimeInMillis();
                    endString = new StringBuilder().append(calendar.getYear()).append("-")
                            .append(calendar.getMonth()).append("-")
                            .append(calendar.getDay()).toString();

                } else {
                    startTimeMillis = calendar.getTimeInMillis();
                    startString = new StringBuilder().append(calendar.getYear()).append("-")
                            .append(calendar.getMonth()).append("-")
                            .append(calendar.getDay()).toString();
                }
                String year = String.valueOf(calendar.getYear());
                String month_day = new StringBuilder().append(calendar.getMonth()).append("月").append(calendar.getDay()).append("日").toString();
                myViewHolder.mTvCorrentDate.setText(new StringBuilder().append(year).append("年").append(month_day).toString());
                myViewHolder.mTvYear.setText(year);
                myViewHolder.mTvDay.setText(month_day);
                myViewHolder.mTvWeek.setText(getWeek(calendar.getWeek()));
            }
        });
    }

    private String getWeek(int weekId) {
        switch (weekId) {
            case 0:
                return "星期日";
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            default:
                return "星期xx";
        }
    }


    class ViewHolder {
        TextView mTvYear;
        TextView mTvDay;
        TextView mTvWeek;
        ImageView mImPre;
        TextView mTvCorrentDate;
        ImageView mImNex;
        TextView mTvCancel;
        TextView mTvConfirm;
        CalendarView mCalendarView;

        public ViewHolder(View convertView) {
            mTvYear = convertView.findViewById(R.id.tv_year);
            mTvDay = convertView.findViewById(R.id.tv_day);
            mTvWeek = convertView.findViewById(R.id.tv_week);
            mImPre = convertView.findViewById(R.id.im_pre);
            mTvCorrentDate = convertView.findViewById(R.id.tv_corrent_date);
            mImNex = convertView.findViewById(R.id.im_nex);
            mCalendarView = convertView.findViewById(R.id.calendarView);
            mTvCancel = convertView.findViewById(R.id.tv_cancel);
            mTvConfirm = convertView.findViewById(R.id.tv_confirm);
        }
    }
}
